package com.ponhu.ea2.stockcenter.provider.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ponhu.ea2.common.basic.model.PagedResult;

import java.time.LocalDateTime;
import java.time.Period;
import java.util.List;

/**
 * @author Wang
 * @date 2021/10/20 10:18 上午
 */
public class DataConverterUtils {
    /**
     * 分页对象转VO，返回PagedResult工具方法
     * @param page 分页对象结果集
     * @param records 转换后的VO LIST数据
     * @param <T> VO对象
     * @return
     */
    public static <T> PagedResult<T> convertToPagedResult(IPage page, List<T> records) {
        if(page == null){
            return null;
        }
        PagedResult<T> pageResult = new PagedResult<>();
        pageResult.setPageIndex((int) page.getCurrent());
        pageResult.setPageSize((int) page.getSize());
        pageResult.setTotal(page.getTotal());
        pageResult.setPageCount((int) page.getPages());
        pageResult.setResult(CollectionUtils.isEmpty(records) ? null : records);
        return pageResult;
    }

    public static String convertToString(Integer value) {
        return convertToString(value, null);
    }

    public static String convertToString(Integer value, String defaultValue) {
        return value == null ? defaultValue : value.toString();
    }

    public static String convertToString(Long value) {
        return convertToString(value, null);
    }

    public static String convertToString(Long value, String defaultValue) {
        return value == null ? defaultValue : value.toString();
    }

    public static int calcDayOfTwoDatetime(LocalDateTime firstWarehouseTime, LocalDateTime lastWarehouseTime) {
        if(firstWarehouseTime == null){
            return 0;
        }
        if(lastWarehouseTime == null){
            lastWarehouseTime = LocalDateTime.now();
        }
        int res = Period.between(firstWarehouseTime.toLocalDate(), lastWarehouseTime.toLocalDate()).getDays();
        if(res == 0){
            return 1;
        }else {
            return res;
        }
    }
}
